use sea_orm_migration::prelude::*;

#[derive(DeriveMigrationName)]
pub struct Migration;

#[async_trait::async_trait]
impl MigrationTrait for Migration {
    async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> {
        manager
            .create_table(
                Table::create()
                    .table(SysOrg::Table)
                    .if_not_exists()
                    .col(
                        ColumnDef::new(SysOrg::Id)
                            .big_integer()
                            .not_null()
                            .auto_increment()
                            .primary_key(),
                    )
                    .col(ColumnDef::new(SysOrg::Pid).big_integer())
                    .col(ColumnDef::new(SysOrg::Name).string().string_len(100).not_null())
                    .col(ColumnDef::new(SysOrg::Code).string().string_len(100))
                    .col(ColumnDef::new(SysOrg::Sort).integer())
                    .col(ColumnDef::new(SysOrg::LeaderId).big_integer())
                    .col(ColumnDef::new(SysOrg::TenantId).big_integer())
                    .col(ColumnDef::new(SysOrg::Creator).big_integer())
                    .col(ColumnDef::new(SysOrg::CreatedAt).timestamp_with_time_zone())
                    .col(ColumnDef::new(SysOrg::Updater).big_integer())
                    .col(ColumnDef::new(SysOrg::UpdatedAt).timestamp_with_time_zone())
                    .col(ColumnDef::new(SysOrg::Version).integer().default(0))
                    .col(ColumnDef::new(SysOrg::Deleted).integer().default(0))
                    .to_owned(),
            )
            .await
    }

    async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
        manager
            .drop_table(Table::drop().table(SysOrg::Table).to_owned())
            .await
    }
}

#[derive(DeriveIden)]
enum SysOrg {
    Table,
    Id,
    Pid,
    Name,
    Code,
    Sort,
    LeaderId,
    TenantId,
    Creator,
    CreatedAt,
    Updater,
    UpdatedAt,
    Version,
    Deleted,
}